<?php

if(!defined('IN_UCHOME')) {
	exit('Access Denied');
}

//检查信息
$op = empty($_GET['op'])?'':$_GET['op'];

if($op == 'atuser') {

	//关注人物
	if(submitcheck('atusersubmit')){
		$buid = empty($_POST['auid'])?0:intval($_POST['auid']);
		if($buid){
			$query = $_SGLOBAL['db']->query("SELECT uid,username FROM ".tname('space')." WHERE uid = $buid LIMIT 1");
			while ($value = $_SGLOBAL['db']->fetch_array($query)) {
				$b_username = $value['username'];
			}

			$setarr = array(
				'b_uid' => $buid,
				'b_username' => $b_username,
				'uid' => $_SGLOBAL['supe_uid'],
				'username' => $_SGLOBAL['supe_username'],
				'dateline' => $_SGLOBAL['timestamp']
			);
			$atid = inserttable('attention', $setarr, 1);
			if($atid){
				$query = $_SGLOBAL['db']->query("SELECT attention_user FROM ".tname('spacefield')." WHERE uid = ".$_SGLOBAL['supe_uid']." LIMIT 1");
				$result = $_SGLOBAL['db']->fetch_array($query);
				$at_user = ($result['attention_user'] == '')?$buid:$result['attention_user'].','.$buid;
				updatetable('spacefield', array('attention_user'=>$at_user), array('uid'=>$_SGLOBAL['supe_uid']));

				include_once(S_ROOT.'./source/function_feed.php');
				feed_publish($atid, 'auserid', 1);
				showmessage('attentioned_success', $_POST['refer'], 1000);
			}else{
				showmessage('do_failed', $_POST['refer'], 2000);
			}

		}else{
			showmessage('attention_uid_not_in', $_POST['refer'], 1000);
		}
	}else{
		$auid = empty($_GET['auid'])?'0':intval($_GET['auid']);
		$query = $_SGLOBAL['db']->query("SELECT id FROM ".tname('attention')." WHERE b_uid = ".$auid." AND uid = ".$_SGLOBAL['supe_uid']);
		if($result = $_SGLOBAL['db']->fetch_array($query)){
			showmessage('attentioned_user');
		}
	}
} elseif ($op == 'atuser_cancel'){

	//取消关注用户
	if(submitcheck('atuser_cancelsubmit')){
		$auid = empty($_POST['auid'])?0:intval($_POST['auid']);
		if($auid){

			//删除
			$query = $_SGLOBAL['db']->query("DELETE FROM ".tname('attention')." WHERE uid='".$_SGLOBAL['supe_uid']."' AND b_uid='$auid'");
			$_SGLOBAL['db']->query("DELETE FROM ".tname('feed')." WHERE uid='".$_SGLOBAL['supe_uid']."' AND id='$auid' AND idtype='auserid'");

			if($query){
				$query = $_SGLOBAL['db']->query("SELECT attention_user FROM ".tname('spacefield')." WHERE uid = ".$_SGLOBAL['supe_uid']." LIMIT 1");
				$result = $_SGLOBAL['db']->fetch_array($query);
				$at_user = ($result['attention_user'] == '')?'':array_diff(explode($result['attention_user']),array($auid));
				updatetable('spacefield', array('attention_user'=>$at_user), array('uid'=>$_SGLOBAL['supe_uid']));

				showmessage('attention_cancel_success', $_POST['refer'], 1000);
			}else{
				showmessage('do_failed', $_POST['refer'], 2000);
			}

		}else{
			showmessage('attention_uid_not_in', $_POST['refer'], 1000);
		}
	}else{
		$auid = empty($_GET['auid'])?'0':intval($_GET['auid']);
		$query = $_SGLOBAL['db']->query("SELECT id FROM ".tname('attention')." WHERE b_uid = ".$auid." AND uid = ".$_SGLOBAL['supe_uid']);
		if(!$query){
			showmessage('attention_not_user');
		}
	}
} elseif ($op == 'atcomp'){

	//关注公司
	if(submitcheck('atcompsubmit')){
		$acid = empty($_POST['acid'])?0:intval($_POST['acid']);
		if($acid){
			$query = $_SGLOBAL['db']->query("SELECT tagid,tagname FROM ".tname('mtag')." WHERE tagid = $acid LIMIT 1");
			while ($value = $_SGLOBAL['db']->fetch_array($query)) {
				$comp_name = $value['tagname'];
			}

			$setarr = array(
				'comp_id' => $acid,
				'comp_name' => $comp_name,
				'uid' => $_SGLOBAL['supe_uid'],
				'username' => $_SGLOBAL['supe_username'],
				'dateline' => $_SGLOBAL['timestamp']
			);
			$atid = inserttable('attention', $setarr, 1);
			if($atid){
				$query = $_SGLOBAL['db']->query("SELECT attention_comp FROM ".tname('spacefield')." WHERE uid = ".$_SGLOBAL['supe_uid']." LIMIT 1");
				$result = $_SGLOBAL['db']->fetch_array($query);
				$at_comp = $result['attention_comp'] == ''?$acid:$result['attention_comp'].','.$acid;
				updatetable('spacefield', array('attention_comp'=>$at_comp), array('uid'=>$_SGLOBAL['supe_uid']));

				include_once(S_ROOT.'./source/function_feed.php');
				feed_publish($atid, 'acompid', 1);
				showmessage('attentioned_success', $_POST['refer'], 1000);
			}else{
				showmessage('do_failed', $_POST['refer'], 2000);
			}

		}else{
			showmessage('attention_uid_not_in', $_POST['refer'], 1000);
		}
	}else{
		$acid = empty($_GET['acid'])?'0':intval($_GET['acid']);
		$query = $_SGLOBAL['db']->query("SELECT id FROM ".tname('attention')." WHERE comp_id = ".$acid." AND uid = ".$_SGLOBAL['supe_uid']);
		if($result = $_SGLOBAL['db']->fetch_array($query)){
			showmessage('attentioned_comp');
		}
	}
} elseif ($op == 'atcomp_cancel'){

	//取消关注公司
	if(submitcheck('atcomp_cancelsubmit')){
		$acid = empty($_POST['acid'])?0:intval($_POST['acid']);
		if($acid){

			//删除
			$query = $_SGLOBAL['db']->query("DELETE FROM ".tname('attention')." WHERE uid='".$_SGLOBAL['supe_uid']."' AND comp_id='$acid'");
			$_SGLOBAL['db']->query("DELETE FROM ".tname('feed')." WHERE uid='".$_SGLOBAL['supe_uid']."' AND id='$acid' AND idtype='acompid'");

			if($query){
				$query = $_SGLOBAL['db']->query("SELECT attention_comp FROM ".tname('spacefield')." WHERE uid = ".$_SGLOBAL['supe_uid']." LIMIT 1");
				$result = $_SGLOBAL['db']->fetch_array($query);
				$at_comp = ($result['attention_comp'] == '')?'':array_diff(explode($result['attention_comp']),array($acid));
				updatetable('spacefield', array('attention_comp'=>$at_comp), array('uid'=>$_SGLOBAL['supe_uid']));

				showmessage('attention_cancel_success', $_POST['refer'], 1000);
			}else{
				showmessage('do_failed', $_POST['refer'], 2000);
			}

		}else{
			showmessage('attention_uid_not_in', $_POST['refer'], 1000);
		}
	}else{
		$acid = empty($_GET['acid'])?'0':intval($_GET['acid']);
		$query = $_SGLOBAL['db']->query("SELECT id FROM ".tname('attention')." WHERE comp_id = ".$acid." AND uid = ".$_SGLOBAL['supe_uid']);
		if(!$query){
			showmessage('attention_not_comp');
		}
	}
} elseif ($op == 'atship'){

	//关注船只
	if(submitcheck('atshipsubmit')){
		$mmsi = empty($_POST['mmsi'])?0:intval($_POST['mmsi']);
		if($mmsi){

			$setarr = array(
				'ship_mmsi' => $mmsi,
				'ship_name' => trim($_POST['ship_name']),
				'ship_callsign' => trim($_POST['callsign']),
				'uid' => $_SGLOBAL['supe_uid'],
				'username' => $_SGLOBAL['supe_username'],
				'dateline' => $_SGLOBAL['timestamp']
			);
			$atid = inserttable('ship_att', $setarr, 1);
			if($atid){
				$query = $_SGLOBAL['db']->query("SELECT attention_ship FROM ".tname('spacefield')." WHERE uid = ".$_SGLOBAL['supe_uid']." LIMIT 1");
				$result = $_SGLOBAL['db']->fetch_array($query);
				$at_ship = ($result['attention_ship'] == '')?$mmsi:$result['attention_ship'].','.$mmsi;
				updatetable('spacefield', array('attention_ship'=>$at_ship), array('uid'=>$_SGLOBAL['supe_uid']));

				include_once(S_ROOT.'./source/function_feed.php');
				feed_publish($atid, 'ashipid', 1);
				showmessage('attentioned_success', $_POST['refer'], 1000);
			}else{
				showmessage('do_failed', $_POST['refer'], 2000);
			}

		}else{
			showmessage('attention_ship_not_in', $_POST['refer'], 1000);
		}
	}else{
		$mmsi = empty($_GET['mmsi'])?'0':intval($_GET['mmsi']);
		$ship_name = empty($_GET['shipname'])?'':$_GET['shipname'];
		$callsign = empty($_GET['callsign'])?'':$_GET['callsign'];
		$query = $_SGLOBAL['db']->query("SELECT id FROM ".tname('ship_att')." WHERE ship_mmsi = ".$mmsi." AND uid = ".$_SGLOBAL['supe_uid']);
		if($result = $_SGLOBAL['db']->fetch_array($query)){
			showmessage('attentioned_ship');
		}else{
			$query = $_SGLOBAL['db']->query("SELECT COUNT(*) as num FROM ".tname('ship_att')." WHERE uid = ".$_SGLOBAL['supe_uid']);
			$result = $_SGLOBAL['db']->fetch_array($query);
			if(intval($result['num'])>=10){
				showmessage('attentioned_is_max');
			}
		}
	}
} elseif ($op == 'atship_cancel' || $op == "atship_cancel_k"){

	//取消关注船只
	if(submitcheck('atship_cancelsubmit')){
		$mmsi = empty($_POST['mmsi'])?0:intval($_POST['mmsi']);
		if($mmsi){

			//删除
			$query = $_SGLOBAL['db']->query("DELETE FROM ".tname('ship_att')." WHERE uid='".$_SGLOBAL['supe_uid']."' AND ship_mmsi='$mmsi'");
			$_SGLOBAL['db']->query("DELETE FROM ".tname('feed')." WHERE uid='".$_SGLOBAL['supe_uid']."' AND id='$mmsi'");

			if($query){
				$query = $_SGLOBAL['db']->query("SELECT attention_ship FROM ".tname('spacefield')." WHERE uid = ".$_SGLOBAL['supe_uid']." LIMIT 1");
				$result = $_SGLOBAL['db']->fetch_array($query);
				$at_ship = ($result['attention_ship'] == '')?'':array_diff(explode($result['attention_ship']),array($mmsi));
				updatetable('spacefield', array('attention_ship'=>$at_ship), array('uid'=>$_SGLOBAL['supe_uid']));

				showmessage('attention_cancel_success', $_POST['refer'], 1000);
			}else{
				showmessage('do_failed', $_POST['refer'], 2000);
			}

		}else{
			showmessage('attention_uid_not_in', $_POST['refer'], 1000);
		}
	}else{
		$mmsi = empty($_GET['mmsi'])?'0':intval($_GET['mmsi']);
		$query = $_SGLOBAL['db']->query("SELECT id FROM ".tname('ship_att')." WHERE ship_mmsi = ".$mmsi." AND uid = ".$_SGLOBAL['supe_uid']);
		if(!$query){
			showmessage('attention_not_ship');
		}
	}
}

include_once template("cp_attention");

?>